package cn.lin.domain;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * 分页
 * @author cn.Lin
 * @create 2024-11-12-9:35
 */
@Getter
@Setter
public class PageInfo<T> {

    private List<T> list;

    // 当前页码
    private Integer pageNum;

    // 每页显示的数量
    private Integer pageSize;

    // 总页数
    private Integer totalPage;

    // 总记录数
    private Integer total;

    public PageInfo(List<T> list, int pageNum, int pageSize, int total) {
        this.list = list;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
    }

    public static <T> PageInfo<T> of(List<T> list, int pageNum, int pageSize, int total) {
        // 计算分页的起始索引和结束索引
        int start = (pageNum - 1) * pageSize;
        int end = start + pageSize;

        List<T> paginatedList = list.subList(start, Math.min(end, list.size()));

        return new PageInfo<>(paginatedList, pageNum, pageSize, total);
    }

}
